package bg.btanev.eightyfour.client.view;

import java.util.Date;
import java.util.List;

import bg.btanev.eightyfour.client.presenter.ProfileEventsSearchPresenter.ProfileEventsSearchDisplay;
import bg.btanev.eightyfour.client.utils.SelectionUtils;
import bg.btanev.eightyfour.shared.dto.TagDto;
import bg.btanev.eightyfour.shared.state.ProfileType;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;

/**
 *
 * @author B.Tanev
 *
 */
public class ProfileEventsSearchView extends Composite implements ProfileEventsSearchDisplay {
	private final ListBox tagData;
	private final TextBox queryData;
	private final Button clearButton;
	private final Button searchButton;
	private final DateBox beginDateData;
	private final DateBox endDateData;
	private List<TagDto> tags;
	private ListBox profileTypeData;

	public ProfileEventsSearchView() {
		final VerticalPanel baseContainer = new VerticalPanel();
		baseContainer.setHeight("175px");
		initWidget(baseContainer);

		final CaptionPanel searchQueryPanel = new CaptionPanel("Search Query");
		baseContainer.add(searchQueryPanel);
		searchQueryPanel.setWidth("100%");

		final Grid searchQueryContainer = new Grid(4, 2);
		searchQueryContainer.setCellSpacing(5);
		searchQueryPanel.setContentWidget(searchQueryContainer);
		searchQueryContainer.setSize("5cm", "3cm");

		final Label datesCaption = new Label("Dates");
		datesCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		searchQueryContainer.setWidget(0, 0, datesCaption);
		datesCaption.setWidth("200px");

		final Grid searchPeriodContainer = new Grid(1, 4);
		searchQueryContainer.setWidget(0, 1, searchPeriodContainer);
		searchPeriodContainer.setWidth("600px");

		final Label dateFromCaption = new Label("from");
		searchPeriodContainer.setWidget(0, 0, dateFromCaption);
		dateFromCaption.setWidth("55px");

		beginDateData = new DateBox();
		beginDateData.setFormat(new DefaultFormat(DateTimeFormat.getLongDateFormat()));
		searchPeriodContainer.setWidget(0, 1, beginDateData);
		beginDateData.setWidth("250px");

		final Label dateToCaption = new Label("to");
		searchPeriodContainer.setWidget(0, 2, dateToCaption);
		dateToCaption.setWidth("25px");

		endDateData = new DateBox();
		endDateData.setFormat(new DefaultFormat(DateTimeFormat.getLongDateFormat()));
		searchPeriodContainer.setWidget(0, 3, endDateData);
		endDateData.setWidth("250px");

		final Label tagCaption = new Label("Tag");
		tagCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		searchQueryContainer.setWidget(1, 0, tagCaption);
		tagCaption.setWidth("200px");

		tagData = new ListBox();
		searchQueryContainer.setWidget(1, 1, tagData);
		tagData.setWidth("300px");

		final Label profleTypeCaption = new Label("Profile Type");
		searchQueryContainer.setWidget(2, 0, profleTypeCaption);
		profleTypeCaption.setWidth("200px");

		profileTypeData = new ListBox();
		searchQueryContainer.setWidget(2, 1, profileTypeData);
		profileTypeData.setWidth("300px");

		final Label queryCaption = new Label("Query");
		queryCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		searchQueryContainer.setWidget(3, 0, queryCaption);
		queryCaption.setWidth("200px");

		queryData = new TextBox();
		searchQueryContainer.setWidget(3, 1, queryData);
		queryData.setWidth("600px");

		final HorizontalPanel actionButtonsContainer = new HorizontalPanel();
		actionButtonsContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		actionButtonsContainer.setSpacing(5);
		baseContainer.add(actionButtonsContainer);
		actionButtonsContainer.setWidth("100%");

		clearButton = new Button("Clear");
		actionButtonsContainer.add(clearButton);
		clearButton.setWidth("110px");
		clearButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				clearView();
			}
		});

		searchButton = new Button("Search");
		actionButtonsContainer.add(searchButton);
		searchButton.setWidth("110px");

		ProfileType[] values = ProfileType.values();
		for (ProfileType profileType : values) {
			this.profileTypeData.addItem(profileType.name(), profileType.name());
		}
		this.profileTypeData.setSelectedIndex(-1);
	}

	public HasValue<Date> getBeginDate() {
		return beginDateData;
	}

	public HasValue<Date> getEndDate() {
		return endDateData;
	}

	public ListBox getTagData() {
		return tagData;
	}

	public HasText getQueryData() {
		return queryData;
	}

	public HasClickHandlers getClearButton() {
		return clearButton;
	}

	public HasClickHandlers getSearchButton() {
		return searchButton;
	}

	public ProfileType getSelectedProfileType() {
		ProfileType type = null;
		String value = SelectionUtils.getSelectedValue(this.profileTypeData);
		if (value != null) {
			type = ProfileType.valueOf(value);
		}
		return type;
	}

	public TagDto getSelectedTag() {
		TagDto tag = null;

		int pos = this.tagData.getSelectedIndex();
		if (pos != -1) {
			tag = this.tags.get(pos);
		}

		return tag;
	}

	public void clearView() {
		beginDateData.setValue(null);
		endDateData.setValue(null);
		tagData.setSelectedIndex(-1);
		profileTypeData.setSelectedIndex(-1);
		queryData.setText("");
	}

	public void setTagsData(List<TagDto> tags) {
		this.tags = tags;

		tagData.clear();
		if (this.tags != null && this.tags.size() > 0) {
			for (TagDto tag : this.tags) {
				tagData.addItem(tag.getId());
			}
			tagData.setSelectedIndex(-1);
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}
}
